package com.lely.controller;

import com.lely.entity.Page;
import com.lely.entity.User;
import com.lely.entity.UserRoleVO;
import com.lely.result.Response;
import com.lely.service.UserService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lely
 * @date 2021/4/15 11:24
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * note: 查询单个用户信息
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public Response getuser(@PathVariable("id") Long id) {
        User user = userService.get(id);
        if(user==null){
            return new Response("200","暂无数据",null);
        }
        return new Response(user);
    }

    /**
     * note：分页列表查询用户
     * @param page: 分页相关信息
     * @return
     */
    @PostMapping("/getAll")
    public Response getuserAll(@RequestBody Page page) {
        List<User> all = userService.getAll(page);
        if(all==null||all.size()==0){
            return new Response("200","暂无数据",null);
        }
        return new Response(all);
    }

    /**
     * @param user:新建的用户信息,需要判断用户名是否存在
     * @return
     */
    @PostMapping("/user")
    public Response createuser(@RequestBody User user) {
        Integer count1 = userService.count(user.getUserName(),user.getEmail());
        if(count1 > 0){
            return new Response("-100","添加用户失败，用户名称或邮箱已存在",null);
        }
        Integer count = userService.add(user);
        if(count==1){
            return new Response( null);
        }else{
            return new Response("-100","添加失败，other",null);
        }
    }

    /**
     * @param user：需要修改的信息
     * @return
     */
    @PutMapping("/user")
    public Response updateuser(@RequestBody User user) {
        int count = userService.update(user);
        if(count>0){
            return new Response("200","更新成功",null);
        }
        return new Response("-100","更新失败",null);
    }

    /**
     * @param id: 需要删除的用户 id
     * @return
     */
    @DeleteMapping("/user/{id}")
    public Response delete(@PathVariable("id") Long id) {
        int count = userService.delete(id);
        if(count>0){
            return new Response("200","删除成功",null);
        }
        return new Response("-100","删除失败",null);
    }

    /**
     * @param vo: 需要分配的用户 id 及角色信息
     * @return
     */
    @PostMapping("/userRole")
    public Response userRole(@RequestBody UserRoleVO vo) {
        int count = userService.addUserRole(vo);
        if(count>0){
            return new Response("200","角色权限分配成功",null);
        }
        return new Response("-100","角色权限分配失败",null);
    }

}
